package com.alageek.study.controller;

import com.alageek.study.entity.Employee;
import com.alageek.study.entity.ResultEntity;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class EmployeeHandler {
    @RequestMapping("/provider/get/employee/remote")
    public Employee getEmployeeRemote(HttpServletRequest request){
        int port = request.getServerPort();
        return new Employee(555, "tom555-" + port, 555.55);
    }
    
    @HystrixCommand(fallbackMethod = "getEmpBackup")
    @RequestMapping("/provider/circuit/breaker/get/emp")
    public ResultEntity<Employee> getEmp(@RequestParam("signal") String signal){
        if("bang".equals(signal)){
            throw new RuntimeException();
        }
        return ResultEntity.succeessWithData(new Employee(666, "sam666", 666.66));
    }

    public ResultEntity<Employee> getEmpBackup(@RequestParam("signal") String signal){
        return ResultEntity.failed("circuit break workded, with signal = " + signal);
    }
}
